A memory management technique that provides the illusion of larger memory space than physically available
Virtual memory is a memory management technique used by operating systems to provide the illusion of a larger and contiguous memory space than physically available in the main memory (RAM). It allows programs to operate as if they have access to a large, continuous block of memory, even though physical memory may be limited or fragmented.
Sees a large, contiguous virtual address space
Maps virtual addresses to physical addresses
Actual RAM installed in the system
Virtual memory creates the illusion of more memory than physically exists
Virtual memory relies on several key concepts to function effectively:
Extends the address space visible to a program beyond actual physical memory
Loads portions of a program only when actively needed
Occurs when accessed memory is not in main memory
Maps virtual addresses to physical addresses
Isolates memory space of different processes
Virtual memory extends the address space visible to a program beyond the actual physical memory installed on the computer. Each program sees a virtual address space that starts from zero and goes up to the maximum addressable limit, defined by the architecture and operating system.
The range of virtual addresses that a process can use. Typically much larger than physical memory.
The process of converting virtual addresses to physical addresses using page tables.
Programs see a contiguous block of memory even if physical memory is fragmented.
Virtual memory uses demand paging, a strategy where portions of a program's code and data are loaded into the main memory only when they are actively needed. This minimizes the amount of physical memory required to run programs and optimizes the usage of available resources.
Program attempts to access memory
System checks if required page is in memory
If not in memory, load from disk
Program resumes with required data
When a program accesses a portion of memory that is not currently in the main memory but resides in the virtual memory, a page fault occurs. The operating system then retrieves the required data from the secondary storage (usually the hard disk) into the main memory and updates the page tables to reflect this mapping.
Hardware detects that a required page is not in physical memory
Operating system takes control to handle the page fault
Required page is loaded from secondary storage
Page tables are updated to reflect the new mapping
Virtual memory relies on page tables to manage the mapping between virtual addresses used by programs and physical addresses in the main memory. These tables store information about which pages of memory are currently resident in physical memory and facilitate quick lookups during address translation.
Page tables are data structures that map virtual page numbers to physical frame numbers
During address translation, the CPU uses the page table to find the physical location
Large systems use hierarchical page tables to reduce memory overhead
Virtual memory systems provide memory protection mechanisms to isolate and protect the memory space of different processes from unauthorized access. This ensures that each program operates within its designated memory boundaries, enhancing system security and stability.
Page tables include permission bits that control read, write, and execute access
Prevents one process from accessing another's memory without authorization
Protects system memory and critical data from malicious or accidental modification
Allows efficient utilization of physical memory by dynamically swapping data between main memory and secondary storage as needed, optimizing overall system performance.
Enables the execution of large applications that require more memory than available physical RAM, enhancing the capabilities of modern software systems.
Programmers can write code without worrying about physical memory limitations, as the operating system manages memory allocation and paging transparently.
Virtual memory is implemented by the operating system using a combination of hardware support (such as memory management units in CPUs) and software algorithms (like page replacement policies). Efficient management of virtual memory requires balancing factors such as page size, page replacement algorithms (e.g., LRU - Least Recently Used), and disk I/O performance to minimize overhead and maximize system responsiveness.
Memory Management Units (MMUs) in CPUs handle address translation and page fault detection
Algorithms like LRU, FIFO, and Clock determine which pages to evict when memory is full
Efficient disk access patterns and caching strategies to minimize page load times
Paging divides physical memory into fixed-size blocks called pages and logical memory into blocks of the same size called frames. Pages are the unit of data transfer between secondary storage and main memory.
Each process has a page table that maps virtual pages to physical frames. The page table typically resides in main memory and is managed by the operating system. It translates virtual addresses generated by the CPU into physical addresses.
When a program references a page not currently in main memory (a page fault occurs), the operating system loads the required page from secondary storage (e.g., disk) into a free frame in main memory. This process is known as demand paging.
If all frames are occupied and a page fault occurs, the operating system must replace a page in main memory with the required page. Various algorithms like Least Recently Used (LRU), First-In-First-Out (FIFO), and Clock are used for page replacement decisions.
Paging allows efficient use of physical memory by allocating memory on-demand, supports memory protection through page-level permissions, and simplifies memory allocation by using fixed-size pages.
Segmentation divides a program's address space into variable-sized logical segments (such as code, data, stack) rather than fixed-size pages.
Each segment is mapped to a segment table entry that stores the base address and size of the segment in main memory. The segment table is typically stored in the CPU and is indexed by a segment number obtained from the virtual address.
When a virtual address is generated by the CPU, the segment number is used to index the segment table to retrieve the base address of the segment. The offset within the segment is then added to the base address to obtain the physical address.
Similar to page faults, segmentation faults occur when a program attempts to access a segment that is not present in main memory or violates memory protection rules.
Segmentation allows for more flexible memory allocation than paging, as segments can vary in size and type (code, data, stack). It supports modular program design and simplifies memory management by providing a hierarchical view of memory.
| Aspect | Paging | Segmentation |
|---|---|---|
| Granularity | Uses fixed-size pages | Uses variable-sized segments |
| Address Translation | Translates virtual addresses to physical addresses using page tables | Uses segment tables to map segments to physical memory |
| Flexibility | Less flexible due to fixed page sizes | More flexible memory allocation with variable segment sizes |
| Fragmentation | Can cause internal fragmentation | Can cause external fragmentation |
| Memory View | Linear view of memory |
Modern virtual memory systems often combine paging and segmentation techniques to leverage their respective advantages. This hybrid approach, known as paged segmentation or segmented paging, allows for both flexible memory allocation and efficient use of physical memory.
First divides memory into logical segments, then each segment is divided into fixed-size pages
Virtual address is translated using both segment table and page table
Combines logical organization of segmentation with efficient memory management of paging
This combined approach provides the best of both worlds: the logical organization and protection benefits of segmentation, along with the efficient memory utilization and simplified allocation benefits of paging. It is widely used in modern operating systems like Windows, Linux, and macOS to provide robust and efficient memory management.